package com.ygqh.baby.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.ao.SendStatus;
import com.ygqh.baby.mapper.YgOrderExtMapper;
import com.ygqh.baby.po.YgOrderExt;
import com.ygqh.baby.po.YgOrderExtExample;
import com.ygqh.baby.service.YgOrderDetailService;
import com.ygqh.baby.service.YgOrderExtService;

@Service
public class YgOrderExtServiceImpl implements YgOrderExtService {

	@Autowired
	private YgOrderExtMapper ygOrderExtMapper;
	@Autowired
	private YgOrderDetailService ygOrderDetailService;

	@Override
	public List<YgOrderExt> find() {
		YgOrderExtExample example = new YgOrderExtExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygOrderExtMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgOrderExtExample example = new YgOrderExtExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygOrderExtMapper.countByExample(example);
	}

	@Override
	public YgOrderExt findById(Long id) {
		return ygOrderExtMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(YgOrderExt ygOrderExt) {
		return ygOrderExtMapper.insertSelective(ygOrderExt);
	}

	@Override
	public int update(YgOrderExt ygOrderExt) {
		return ygOrderExtMapper.updateByPrimaryKeySelective(ygOrderExt);
	}

	@Override
	public ResultSet<YgOrderExt> search(QueryInfo queryInfo, String q) {
		List<YgOrderExt> list = ygOrderExtMapper.selectSuper(queryInfo, q);
		int count = ygOrderExtMapper.countSuper(q);
		ResultSet<YgOrderExt> resultSet = new ResultSet<YgOrderExt>(count, list);
		return resultSet;
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		YgOrderExtExample example = new YgOrderExtExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		YgOrderExt record = new YgOrderExt();
		record.setStatus(DataStatus.Delete);

		return ygOrderExtMapper.updateByExampleSelective(record, example);
	}

	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[] { id });
	}

	@Override
	public int saveOrUpdate(YgOrderExt ygOrderExt) {
		int r = 0;
		if (ygOrderExt.getId() != null) {
			r = this.update(ygOrderExt);
		} else {
			r = this.save(ygOrderExt);
		}
		return r;
	}

	@Override
	public Boolean isAllSend(Long orderId) {
		// DOTO 未完成。。。 产生退换货部分 如何判定 各种场景

		List<Long> list = ygOrderExtMapper.notSendCompleteOrder(orderId);
		if (CollectionUtils.isEmpty(list)) {

		}
		return false;
	}

	public List<YgOrderExt> getAllSendOrder(Long... orderIds) {
		// DOTO
		List<Long> list = ygOrderExtMapper.notSendCompleteOrder(orderIds);
		if (CollectionUtils.isEmpty(list)) {
		}

		return null;
	}

	@Override
	public int updateSendStatus(Long orderId, SendStatus sendStatus, Date sendTime) {

		YgOrderExt ygOrderExt = new YgOrderExt(orderId);
		this.update(ygOrderExt);
		return 0;
	}

}
